<?php

namespace App\Http\Controllers\Admin\RandomCheck;

use App\Exceptions\LogicException;
use App\Http\Controllers\Controller;
use App\Models\Adminer\Admins;
use App\Models\Projects\Product;
use App\Models\RandomCheck\RandomCheckLog;
use App\Utils\QueryUtil;
use App\Utils\ResUtil;

class LogsController extends Controller
{
    public function list()
    {
        $request = request()->all();
        $query = RandomCheckLog::where(function($query) use($request) {
            QueryUtil::selectBuild($query, $request, [
                ['product_id', '='],
                ['remark', 'like', 'remark'],
                ['modules', 'like', 'modules'],
                ['note', 'like', 'note'],
                ['created_adminer', 'like', 'created_adminer'],
                ['updated_adminer', 'like', 'updated_adminer'],
                ['content', 'like', 'content'],
                ['res', 'like', 'res'],
                ['start_at', '>=', 'created_at'],
                ['end_at', '<=', 'created_at'],
                ['id', '='],
            ]);
        });

        $total = $query->count();
        $models = QueryUtil::queryList($query);
        $list = [];
        foreach($models as $i => $v) {
            $list[$i] = $v?->label()?->SignOssUrlToExplodeImgs()?->Explode()?->toArray();
        }
        $hasMore = QueryUtil::hasMore(sizeof($list));
        return $this->ok(compact('list', 'total', 'hasMore'));
    }

    public function edit()
    {
        $data = request()->all();
        $rule = [
            'id' => '',
            'product_id' => 'required',
            'modules' => 'array|required',
            'content' => 'string|required',
            'remark' => 'string|required',
            'note' => 'string|required',
            'res' => 'string|required',
            'info_imgs' => 'array|required'
        ];
        $validator = validatorService()->checkValidator($rule, $data, RandomCheckLog::$fillables);
        if ($validator['code'] != 200) {
            throw new LogicException($validator['data']);
        }

        if($data['id'] ?? '') {
            $project = RandomCheckLog::find($data['id']);
            if(!$project) {
                throw new LogicException(ResUtil::NOT_DATA);
            }
        } else {
            $project = new RandomCheckLog();
            $project->created_adminer = Admins::getInfoById($this->authAdmin()->id);
        }
        $product = Product::find($data['product_id']);
        if(!$product) {
            throw new LogicException('产品不存在');
        }
        $info_imgs = implode(',', $data['info_imgs']);
        $modules = implode(',', $data['modules']);
        $project->fill([
            'product_id' => $product->id,
            'content' => $data['content'],
            'remark' => $data['remark'],
            'note' => $data['note'],
            'modules' => $modules,
            'res' => $data['res'],
            'info_imgs' => $info_imgs,
            'updated_adminer' => Admins::getInfoById($this->authAdmin()->id)
        ]);
        $project->save();
        return $this->ok();
    }
}
